﻿using CMS.BaseEntity;
using ElectronicSelling.BusinessLogic;
using ElectronicSelling.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicSelling.UserControl
{
    public partial class Authentication : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Register()
        {
            User user = new User();
            user.UserName = txtName.Text;
            user.Password = Library.GennerateToMD5(txtPass.Text);
            user.FullName = txtFullName.Text;
            user.DateOfBirth = txtDOB.Text;
            user.Email = txtEmail.Text;
            user.Address = txtAddress.Text;
            //try
            //{
                if (HomeAccountLogic.IsInsertCustomer(user))
                {
                    DataTable dt = HomeAccountLogic.GetUserLogin(user);
                    DataRow dr = dt.Rows[0];
                    Session["UserName"] = dr["UserName"];
                    Session["UserId"] = dr["UserId"];
                    Session["FullName"] = dr["FullName"];
                }
            //}
            //catch (Exception e)
            //{
            //    //Response.Write(e.Message);
            //    Response.Redirect(Directory.Error);
            //}
        }

        protected void Authenticate()
        {
            User user = new User();
            user.UserName = txtUserName.Text;
            user.Password = Library.GennerateToMD5(txtPassword.Text);
            try
            {
                if (HomeAccountLogic.IsAuthentication(user))
                {
                    DataTable dt = HomeAccountLogic.GetUserLogin(user);
                    DataRow dr = dt.Rows[0];
                    Session["UserId"] = dr["UserId"];
                    Session["UserName"] = dr["UserName"];
                    Session["Role"] = dr["Role"];
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", "window.location.reload();", true);
                }
            }
            catch (Exception e)
            {
                //Response.Write(e.Message);
                Response.Redirect(Directory.Error);
            }
        }

        protected void LoginVal_ServerValidate(object source, ServerValidateEventArgs args)
        {
            User user = new User();
            user.UserName = txtUserName.Text;
            user.Password = Library.GennerateToMD5(txtPassword.Text);
            if (HomeAccountLogic.IsAuthentication(user))
            {
                args.IsValid = true;
            }
            else
                args.IsValid = false;
        }

        protected void RegisterVal_ServerValidate(object source, ServerValidateEventArgs args)
        {
            User user = new User();
            user.UserName = txtName.Text;
            user.Password = Library.GennerateToMD5(txtPass.Text);
            user.FullName = txtFullName.Text;
            user.DateOfBirth = txtDOB.Text;
            user.Email = txtEmail.Text;
            user.Address = txtAddress.Text;
            user.Phone = txtPhone.Text;
            if (HomeAccountLogic.IsInsertCustomer(user))
            {
                args.IsValid = true;
            }
            else
                args.IsValid = false;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Authenticate();
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
                Register();
        }

        protected void txtName_TextChanged(object sender, EventArgs e)
        {
            lblName.Visible = true;
            User user = new User();
            user.UserName = txtName.Text;
            if (HomeAccountLogic.IsExistCustomer(user))
            {
                lblName.CssClass = "error_text";
                lblName.Text = ConstantDefault.Exist_Customer;
            }
            else
            {
                lblName.CssClass = "ready_text";
                lblName.Text = ConstantDefault.Ready_Customer;
                txtPass.Focus();
            }
        }
    }
}